<template>
  <el-dialog
    title="分配角色"
    :visible="showRoleAssign"
    @close="handleClose"
  >
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" v-model="roleIds" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button v-loading="loading" type="primary" size="small" @click="handleConfirm">确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { assignRolesApi } from '@/api/employees.js'
export default {
  name: 'Hrsaas35sAssignRole',
  props: {
    showRoleAssign: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: [], // 角色列表
      loading: false
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:showRoleAssign', false)
    },
    async handleConfirm() {
      try {
        this.loading = true
        await assignRolesApi({ id: this.$attrs['user-id'], roleIds: this.roleIds })
        this.$message.success('分配成功')
        this.handleClose()
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
